//
//  JMAcademInfoViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/7.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMAcademInfoViewController.h"
#import <UIImageView+WebCache.h>
#import "JMAppNetworkClient.h"
#import "NSString+date.h"
#import "JMLoginViewController.h"

@interface JMAcademInfoViewController ()<UIWebViewDelegate>
{

    
}

@property (nonatomic, strong) UIScrollView *backScrollView;

@property (nonatomic, strong) UIImageView *logoImageView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation JMAcademInfoViewController


- (UIScrollView *)backScrollView
{
    if (!_backScrollView) {
        _backScrollView = [[UIScrollView alloc]init];
        _backScrollView.backgroundColor = [UIColor clearColor];
        _backScrollView.frame = CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT);
    }
    return _backScrollView;
}

- (UIImageView *)logoImageView
{
    if (!_logoImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor clearColor];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.mode.logoString]];
        _logoImageView = imageView;
    }
    return _logoImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.text =self.mode.title;
        label.font = [UIFont systemFontOfSize:14.0f];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc]init];
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.scrollEnabled = NO;
        _webView.delegate=self;
    }
    return _webView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationItem.title =@"院校详情";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.backScrollView];
    
    UIView *topView= [[UIView alloc]initWithFrame:CGRectMake(-0.5, 15, UI_SCREEN_WITH + 1, UI_SCREEN_WITH *0.234375)];
    topView.backgroundColor = [UIColor whiteColor];
    topView.layer.masksToBounds = YES;
    topView.layer.borderWidth= 0.5;
    topView.layer.borderColor = [UIColorFromRGB(0xc4c4c4) CGColor];
    [self.backScrollView addSubview:topView];
    
    [topView addSubview:self.logoImageView];
    self.logoImageView.frame = CGRectMake(UI_SCREEN_WITH *0.0375, (topView.frameHeight - UI_SCREEN_WITH *0.16875)/2.0, UI_SCREEN_WITH *0.16875, UI_SCREEN_WITH *0.16875);
    [topView addSubview:self.titleLabel];
    self.titleLabel.frame = CGRectMake(self.logoImageView.frameRight + 5, self.logoImageView.frameTop + 5, 150,UI_SCREEN_WITH *0.0546875);
    
    float leftPoin = self.titleLabel.frameLeft;
    NSArray *dataArray = [self getMarkArray];
    for(int i =0 ;i <dataArray.count;i++){
        NSString *title = dataArray[i];
        CGSize size = [title sizeWithFont:[UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125] andMaxSize:CGSizeMake(320, UI_SCREEN_WITH *0.0625)];
        
        UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(leftPoin, self.titleLabel.frameBottom + 10, size.width + 5,UI_SCREEN_WITH *0.046875)];
        la.text = title;
        la.layer.masksToBounds =YES;
        la.layer.cornerRadius = 3.0;
        la.textAlignment =NSTextAlignmentCenter;
        la.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.03125];
        la.backgroundColor = UIColorFromRGB(0xe9e9e9);
        la.textColor = [UIColor blackColor];
        [topView addSubview:la];
        leftPoin =la.frameRight + 3;
    }

    [self.backScrollView addSubview:self.webView];
    self.webView.frame = CGRectMake(0, topView.frameBottom + 15, UI_SCREEN_WITH, UI_SCREEN_HEIGHT - topView.frameBottom - 64 - 15);
    
    [JMTools showAlertView:self.view];
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(cf.user){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [dict setObject:[NSString stringWithFormat:@"%d",self.mode.collegeId] forKey:@"id"];
        [JMAppNetworkClient SchoolIfoWithInfo:dict complete:^(id data, NSError *error) {
            [JMTools hiddenAlerView:self.view];
            if([data[@"status"] integerValue] == 1){
                NSDictionary *downDict =(NSDictionary *)data;
                [JMTools showAlertView:self.view message:data[@"message"]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                    NSString *str =downDict[@"info"][@"jieshao"];
                    [self.webView loadHTMLString:str baseURL:nil];
                    });
            }else{
                [JMTools showAlertView:self.view message:data[@"message"]];
                JMLoginViewController *vc =[JMLoginViewController new];
                JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
                [self presentViewController:nav animated:YES completion:^{
                    
                }];

            }
        }];
    }
}

-(NSArray *)getMarkArray{
    NSString *str = [self.mode.levelString stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
    
    NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    NSString * theString = [filteredArray componentsJoinedByString:@" "];
    
    NSMutableArray * array = (NSMutableArray *)[theString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"[]"]];
    for(int i = 0; i<array.count;i++){
        NSString *subStr =array[i];
        if(subStr.length == 0){
            [array removeObject:subStr];
        }
    }
    return array;
}

#pragma mark webView deleaget
-(void)webViewDidFinishLoad:(UIWebView *)webView{

    self.webView.frame = CGRectMake(self.webView.frameLeft, self.webView.frameTop, self.webView.frameWidth, self.webView.scrollView.contentSize.height);
    self.backScrollView.contentSize = CGSizeMake(UI_SCREEN_WITH, self.webView.scrollView.contentSize.height + UI_SCREEN_WITH *0.234375 + 15);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
